﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.Utils.Win;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid.Demos.util;
using mendian;
using Luna小店.Utils;
using Luna小店.DTO;

namespace Luna小店.UC.款式选择
{
    public partial class Frm款式 : DevExpress.XtraEditors.XtraForm
    {
        private string categoryId;
        private string name;
        public Frm款式()
        {
            InitializeComponent();
            this.searchLookUpEdit款式选择.Properties.DataSource = FunctionHelper.JsonDeserialization<List<DTO款式>>(RestCall.httpGetMethod(URL.接口url +"/style/getStyleCategoryAll"));
        }

        #region 选择款式
        private void searchLookUpEdit款式选择_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit款式选择.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.categoryId = this.searchLookUpEdit款式选择.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.name = this.searchLookUpEdit款式选择.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                }
            }
            this.gridControl款式详情.DataSource = FunctionHelper.JsonDeserialization<List<DTOstyleCategory>>(RestCall.httpGetMethod(URL.接口url + "/style/getStyleMapByCategory?categoryId=" + this.categoryId));
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit款式选择.ToolTip = null;
            searchLookUpEdit款式选择.EditValue = null;
        }
        private void searchLookUpEdit款式选择_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.name;
        }
        #endregion

        private void simpleButton导入_Click(object sender, EventArgs e)
        {
            DataSet result = ExcelUtil.getExcelDatas();
            if (result == null)
            {
                return;
            }
            DataTable dataTable = result.Tables[0];
            List<DTOstyleCategory> list = new List<DTOstyleCategory>();
            foreach (DataRow dataRow in dataTable.Rows)
            {
                list.Add(new DTOstyleCategory(dataRow));
            }
            new Frm款式导入预览(list, this.categoryId).ShowDialog();
            
        }

        //private void simpleButton导入量体值_Click(object sender, EventArgs e)
        //{
        //    //DataSet result = ExcelUtil.getExcelDatas();
        //    //DataTable dataTable = result.Tables[this.name];
        //    //List<DTOMeasure> list = new List<DTOMeasure>();
        //    //foreach (DataRow dataRow in dataTable.Rows)
        //    //{
        //    //    list.Add(new DTOMeasure(dataRow));
        //    //}
        //    //RestCall.PostHttp(URL.接口url + "/style/?categoryId=" + this.categoryId, FunctionHelper.JsonSerialization(list));
        //}
    }
    public class DTO款式
    {
        public string code { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public DTO款式()
        {
        }
        public DTO款式(string code, string id, string name)
        {
            this.code = code;
            this.id = id;
            this.name = name;
        }
    }
    public class DTOstyleCategory
    {
        public string code { get; set; }
        public string name { get; set; }
        public string styleId { get; set; }
        public DTOstyleCategory()
        {
        }

        public DTOstyleCategory(string styleId, string code, string name)
        {
            this.styleId = styleId;
            this.code = code;
            this.name = name;
        }
        public DTOstyleCategory(DataRow dataRow)
        {
            this.styleId = dataRow["column0"].ToString();
            this.code = dataRow["column1"].ToString();
            this.name = dataRow["column2"].ToString();
        }

    }
}